// pages/yaotang/yaotang.js
var app = getApp()
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.js');
var gcoord = require('../../../../utils/gcoord.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据14833566621605601745
   */
  data: {
    hospital: [],
    hospital_serch: [],
    pages: 0,
    pagesizes: 10,
    ifzj: 0,
    zhenduan: '',
    yongyao: '',
    text:'',
    back_orser:0,
    zid:0
  },
  serch: function (e) {
    console.log(e.detail.value)
    var hospital = this.data.hospital
    var arr = []
    for (var i = 0; i < hospital.length; i++) {
      if (hospital[i].agentname.indexOf(e.detail.value) != -1) {
        arr.push(hospital[i])
      }
    }
    this.setData({
      serch_text: e.detail.value,
      hospital_serch: arr
    })
  },
  close_btn: function (e) {
    this.setData({
      serch_text: ''
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */



  onLoad: function (options) {



    var that = this
    var key_words = options.key_words
    var j_id = options.j_id
    var zid = options.zid
    if(options.back_orser){
      var back_orser = options.back_orser
    }else{
      var back_orser = 0
    }
    
    var inquiry = options.inquiry
    if (options.zhenduan !== undefined) {
      var zhenduan = options.zhenduan
      that.setData({
        zhenduan: zhenduan,
        yongyao: options.yongyao,
        ifzj: options.ifzj
      })
    }


    if (options.conets !== undefined) {
      var conets = JSON.parse(options.conets)
      var text = conets.text
      var text2 = conets.text2
      var upload_picture_list = conets.upload_picture_list
      var typedate = conets.typedate
      that.setData({
        conets: conets,
        text: text,
      })
    }else{
      that.setData({
        text: options.text,
      })
    }
    that.setData({
      key_words: key_words,
      inquiry: inquiry,
      back_orser:back_orser,
      j_id: j_id,
      zid:zid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('2')
    var that = this
    qqmapsdk = new QQMapWX({
      key: 'V7DBZ-K7C22-SXXUJ-CDUE7-AM2LH-AEFCM' // 必填
    });
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        app.util.request({
          'url': "entry/wxapp/index.yao_hospital",
          data: {
            latitude: latitude,
            longitude: longitude,
            page: that.data.pages,
            pagesizes: that.data.pagesizes,
          },
          success: function (res) {
            console.log(res)
            var pages = that.data.pages
            if (res.data.length == 10) {
              pages = pages + 1;
            }
            that.setData({
              pages: pages,
              hospital: that.data.hospital.concat(res.data)
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //查询所有药房
  getall(latitude, longitude) {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.yaofang',
      data: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log(res)
      }
    })
  },
  select(e) {
    var that = this
    var hid = e.currentTarget.dataset.hid
    var key_words = that.data.key_words
    var j_id = that.data.j_id
    var conets = JSON.stringify(that.data.conets)
    var text = that.data.text
    var zhenduan = that.data.zhenduan
    var yongyao = that.data.yongyao
    var ifzj = that.data.ifzj
    var back_orser = that.data.back_orser
    var zid = that.data.zid
    wx.navigateTo({
      url: '/hyb_yl/backstageServices/pages/xuanzeyaopin/xuanzeyaopin?key_words=' + key_words + '&j_id=' + j_id + '&ser_key=' + key_words + '&pinyin=' + key_words + '&inquiry=' + that.data.inquiry + '&conets=' + conets + '&text=' + text + '&hid=' + hid + '&zhenduan=' + zhenduan + '&yongyao=' + yongyao + '&ifzj=' + ifzj+'&back_orser='+back_orser+'&zid='+zid,
    })
  }
})